package com.tjec.project.projectLeaderWorkbeanch.controller;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.service.IBmsBusinessService;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsAttachmentProjectBusiness;
import com.tjec.project.system.service.IBmsAttachmentProjectBusinessService;
import com.tjec.project.system.service.IBmsAttachmentService;
import com.tjec.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文档回收站
 *
 * @author cjj
 * @date 2020-06-05
 */
@Api(value = "项目负责人工作台-文档回收站", tags = {"项目负责人工作台-文档回收站接口"})
@RestController
@RequestMapping("/projectLeader/fileRecycle")
public class BmsFileRecycleController extends BaseController {

    @Autowired
    private IBmsAttachmentService bmsAttachmentService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IBmsProjectService bmsProjectService;

    @Autowired
    private IBmsAttachmentProjectBusinessService bmsAttachmentProjectBusinessService;

    @Autowired
    private IBmsBusinessService bmsBusinessService;

    @Value("${sftp.ip}")
    private String ip;
    @Value("${sftp.http}")
    private String http;

    /**
     * 文档回收站列表
     */
    @ApiOperation("文档回收站列表")
//    @PreAuthorize("@ss.hasPermi('projectLeader:fileRecycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsAttachment bmsAttachment) {
        List<BmsAttachment> result = new ArrayList<>();
        bmsAttachment.setDelFlag("2");
        bmsAttachment.setCategorys(new String[]{"02","03","04","05"});
        String leaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsAttachment.getLeader());
        if(StringUtils.isBlank(leaderNo)){
            return getDataTable(result);
        }
        BmsProject bmsProject = new BmsProject();
        bmsProject.setProjectLeaderNo(leaderNo);
        bmsProject.setEnableFlag("0");
        List<BmsProject> bmsProjects = bmsProjectService.selectBmsProjectList(bmsProject);
        List<String> projectIdList = new ArrayList<>();
        for (BmsProject project : bmsProjects) {
            projectIdList.add(project.getGuid());
        }
        List<String> attIdlist = bmsAttachmentProjectBusinessService.selectBmsAttachmentProjectBusinessListByProjectId(projectIdList);
        if(!CollectionUtils.isEmpty(attIdlist)){
            bmsAttachment.setGuids(attIdlist.toArray(new String[attIdlist.size()]));
            startPage();
            result = bmsAttachmentService.selectBmsAttachmentList(bmsAttachment);
        }
        for (BmsAttachment attachment : result) {
            List<BmsAttachmentProjectBusiness> bmsAttachmentProjectBusinesss = bmsAttachmentProjectBusinessService.selectBmsAttachmentProjectBusinessByGuid(attachment.getGuid());
            if(!CollectionUtils.isEmpty(bmsAttachmentProjectBusinesss)){
                BmsProject bmsProject1 = bmsProjectService.selectBmsProjectByGuid(bmsAttachmentProjectBusinesss.get(0).getProjectId());
                BmsBusiness bmsBusiness = bmsBusinessService.selectBmsBusinessByGuid(bmsAttachmentProjectBusinesss.get(0).getBusinessId());
                attachment.setProjectName(bmsProject1!=null?bmsProject1.getName():"");
                attachment.setBusinessName(bmsBusiness!=null?bmsBusiness.getName():"");
                attachment.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + attachment.getStoragePath());
            }
        }
        return getDataTable(result);
    }

    /**
     * 删除文档回收站文件
     */
    @ApiOperation("删除文档回收站文档")
    @ApiImplicitParam(name = "guids", value = "文档guid集合", required = true, dataType = "Array", paramType = "path")
    @Log(title = "项目负责人人员", businessType = BusinessType.DELETE)
//    @PreAuthorize("@ss.hasPermi('projectLeader:fileRecycle:remove')")
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        BmsAttachment bmsAttachment = new BmsAttachment();
        bmsAttachment.setDelFlag("9");
        for (String guid : guids) {
            bmsAttachment.setGuid(guid);
            bmsAttachmentService.updateBmsAttachment(bmsAttachment);
        }
        return AjaxResult.success();
    }
}
